package nciae.db.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import nciae.db.common.CommonResult;
import nciae.db.pojo.dto.*;
import nciae.db.pojo.entity.Classs;
import nciae.db.pojo.entity.Project;
import nciae.db.pojo.entity.Student;
import nciae.db.pojo.entity.User;
import nciae.db.pojo.vo.ApiGetDateLogVO;
import nciae.db.pojo.vo.ApiStudentLoginVO;
import nciae.db.pojo.vo.ApiTotalScoreVO;
import nciae.db.pojo.vo.StudentVO;
import nciae.db.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 学员管理控制器
 */
@Controller
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/student/index")
    public String index(){
        return "student/index";
    }

//    @GetMapping("/student/index")
//    public String index(){
//        return "student/index";
//    }
    /**

     */
//    @PostMapping("/api/UploadDairyFiles")
//    @ResponseBody
//    public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
//                                           @RequestParam("userId") Long userId,
//                                           @RequestParam("projectId") Long projectId) {
//        return studentService.uploadDairyFile(file, userId, projectId);
//    }

    @PostMapping("/api/login")
    @ResponseBody
    public CommonResult<ApiStudentLoginVO> login(@RequestBody ApiStudentLoginDTO apiStudentLoginDTO){
        CommonResult<ApiStudentLoginVO> result = new CommonResult<>();
        String tel = apiStudentLoginDTO.getUsername();
        String password = apiStudentLoginDTO.getPassword();
        if(tel==null || password==null || StringUtils.isBlank(tel) || StringUtils.isBlank(password)){
            result.setCode(-1);
            result.setMsg("账号或密码错误");
            return result;
        }
        result = studentService.login(tel,password);
        return result;
    }

    @PostMapping("/api/apiVerifyToken")
    @ResponseBody
    public CommonResult<Object> apiVerifyToken(){
        CommonResult<Object> result = new CommonResult<>();
        result.setCode(0);
        return result;
    }


    //日志的返回
    @PostMapping("/api/apiGetDateLog")
    @ResponseBody
    public CommonResult<ApiGetDateLogVO> apiGetDateLog(@RequestBody ApiGetDateLogDTO apiGetDateLogDTO){
        CommonResult<ApiGetDateLogVO> result = new CommonResult<>();
        String date = apiGetDateLogDTO.getDate();
        Long projectId = apiGetDateLogDTO.getProjectId();
        Long userId = apiGetDateLogDTO.getUserId();
        if(projectId == null || userId == null || date==null || StringUtils.isBlank(date)){
            result.setCode(-1);
            result.setMsg("日期不能为空");
            return result;
        }
        result = studentService.apiGetDateLog(userId,projectId,date);
        return result;
    }

    @RequestMapping("/api/score")
    @ResponseBody
    public CommonResult<ApiTotalScoreVO> apiTotalScore(@RequestBody apiTotalScoreDto apiTotalScoreDTO){
        System.out.println(apiTotalScoreDTO.getUserId());

        return studentService.getScoreInfo(apiTotalScoreDTO.getUserId(),apiTotalScoreDTO.getProjectId());
    }

    @PostMapping("/api/revisePassword")
    @ResponseBody
    public CommonResult<Boolean> revisePassword(@RequestBody ApiRevisePwdDTO apiRevisePwdDTO){
        return studentService.revisePassword(apiRevisePwdDTO);
    }

    @ResponseBody
    @RequestMapping("/student/list")
    public CommonResult<List<StudentVO>> list(int page, int limit, String name, String department, String tel, String project, String classs, String expert){
        return studentService.search(page,limit,name, department, tel, project, classs,expert);
    }

    @ResponseBody
    @PostMapping("/student/del")
    public CommonResult<Boolean> del(Integer id){
        return studentService.delete(id);
    }

    @GetMapping("/student/form")
    public ModelAndView form(Integer id, Model model){
        StudentVO studentVo;
        if(id==0){
            studentVo = new StudentVO();
            studentVo.setId(0);
            studentVo.setName("");
            studentVo.setDepartment("");
            studentVo.setTel("");
            studentVo.setUserPwd("");
            studentVo.setProjectId(0);
            studentVo.setClassId(0);
            studentVo.setExpertId(0);
            Classs classs = new Classs();
            classs.setClassName("");
            studentVo.setClasss(classs);
            Project project = new Project();
            project.setProjectName("");
            studentVo.setProject(project);

        }
        else{
            studentVo = studentService.getUserInfo(id);
        }
        ModelAndView view = new ModelAndView();
        view.setViewName("student/form");
        model.addAttribute("model",studentVo);
        return view;
    }

    @ResponseBody
    @PostMapping("/student/save")
    public CommonResult<Boolean> save(StudentUpdateRequest student){
        CommonResult<Boolean> result = new CommonResult<>();
        String name = student.getName();
        if(name == null || StringUtils.isBlank(name)){
            result.setCode(-1);
            result.setMsg("姓名不能为空");
            result.setData(false);
            return result;
        }
        String department = student.getDepartment();
        if(department == null || StringUtils.isBlank(department)){
            result.setCode(-1);
            result.setMsg("工作单位不能为空");
            result.setData(false);
            return result;
        }
        String tel = student.getTel();
        if(tel == null || StringUtils.isBlank(tel)){
            result.setCode(-1);
            result.setMsg("手机号码不能为空");
            result.setData(false);
            return result;
        }

        String project = student.getProject();
        if(project == null || StringUtils.isBlank(project)){
            result.setCode(-1);
            result.setMsg("项目不能为空");
            result.setData(false);
            return result;
        }
        String classs = student.getClasss();
        if(classs == null || StringUtils.isBlank(classs)){
            result.setCode(-1);
            result.setMsg("班级不能为空");
            result.setData(false);
            return result;
        }



        return studentService.save(student);
    }

    // 重置学员密码接口
    @PostMapping("/student/resetPassword")
    @ResponseBody
    public CommonResult<Boolean> resetPassword(@RequestParam Long id) {
        CommonResult<Boolean> result = new CommonResult<Boolean>();
        try {
            boolean isSuccess = studentService.resetPassword(id);
            if (isSuccess) {
                result.setData(true);
                result.setCode(0);
                result.setMsg("密码重置成功");
            } else {
                result.setData(false);
                result.setCode(-1);
                result.setMsg("密码重置失败");
            }
        } catch (Exception e) {
            result.setData(false);
            result.setCode(-1);
            result.setMsg("发生错误: " + e.getMessage());
        }
        return result;
    }

    /**
     * 批量导入学生数据
     */
    @PostMapping("/student/batchImport")
    @ResponseBody
    public CommonResult<Integer> batchImport(@RequestParam("className") String className, @RequestParam("chargerName") String chargerName, @RequestParam("projectName") String projectName, @RequestParam("file") MultipartFile file){
        return studentService.batchImport(className, chargerName, projectName, file);
    }

    @GetMapping("/student/batchImportForm")
    public ModelAndView batchImportForm(Model model){
        ModelAndView view = new ModelAndView();
        view.setViewName("student/batchImportForm.html");
        return view;
    }

    /**
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/student/assignStudentToExpert")
    public CommonResult<Boolean> assignStudentToExpert(@RequestBody AssignRequest request){
        return studentService.assignStudentsToExpert(request.getStudentIds(), request.getExpertId());
    }

    @GetMapping("/student/assignExpertForm")
    public ModelAndView assignExpertForm(Model model){
        ModelAndView view = new ModelAndView();
        view.setViewName("student/assignExpertForm");
        return view;
    }

    @PostMapping("/api/apiInsertAttendence")
    @ResponseBody
    public CommonResult<Boolean> apiInsertAttendence(@RequestBody ApiInsertAttendenceDTO apiInsertAttendenceDTO){
        return studentService.apiInsertAttendence(apiInsertAttendenceDTO);
    }



}
